package com.jiejie.controller;


import com.jiejie.entity.Comments;
import com.jiejie.entity.Customer;
import com.jiejie.entity.Likes;
import com.jiejie.entity.LoginCustomer;
import com.jiejie.service.ICommentsService;
import com.jiejie.service.ILikesService;
import com.jiejie.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.function.ServerResponse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 辰广
 * @since 2022-10-05
 */
@RestController

public class LikesController {
    @Autowired
    private ILikesService likesService;
    @Autowired
    private ICommentsService commentsService;
    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping("getLikesByCustomerId")
    public Map<String,Object> getLikeCommentIdByCustomerId(HttpServletRequest request){
        String token=request.getHeader("token");
        System.out.println(token);
        LoginCustomer loginCustomer=null;
        List<Likes> likesList=null;
        List<Likes> likesListFromResis=null;
        if(!token.equals("null")){
            loginCustomer= TokenUtil.parseToken(token);
            likesList=likesService.getLikesByCustomer(loginCustomer.getCustomerId());//数据库中
            likesListFromResis=likesService.getLikedDataFromRedisForJsp();//有的状态是1,而且是所有用户的点赞记录
            Iterator<Likes> it=likesListFromResis.iterator();
            while (it.hasNext()){
                Likes likes=it.next();
                System.out.println(likes);
                if(!likes.getCustomerId().equals(loginCustomer.getCustomerId()))
                it.remove();
            }
        }
        List<Comments> commentsList=likesService.getLikesNumFromRedisFrojsp();


        //假数据
//        Likes likes=new Likes();
//        likes.setCustomerId(101);
//        likes.setCommentsId(11);
//        likes.setLikesStatus(0);
       // likesListFromResis.add(likes);
        Map<String,Object> listMap=new LinkedHashMap<>();
        listMap.put("commentsList",commentsList);
        listMap.put("likesList",likesList);
        listMap.put("likesListFromResis",likesListFromResis);
        System.out.println("redis中的likes:"+likesListFromResis);
        System.out.println("redis中所有hashkey:");
        System.out.println( redisTemplate.opsForHash().entries("MAP_COMMENT_LIKED"));
        return listMap;
    }
    @GetMapping("redisDecreaseLike")
    public Integer redisDecreaseLikes(@RequestParam("likesNum") Integer likesNum,
                                     @RequestParam("commentId") Integer commentId,
                                     @RequestParam(value = "movieId") Integer movieId,
                                     HttpServletRequest request){
        LoginCustomer loginCustomer =null;
        Integer customerId=null;
        String token=request.getHeader("token");
        if (!token.equals("null")) {
            //1.登陆用户数据
            loginCustomer = TokenUtil.parseToken(token);
           customerId = loginCustomer.getCustomerId();
        }
        String key="commentUserLike_"+commentId;//该评论在Redis中的点赞数
        if(likesNum==1){//点赞是从0-1-0
            likesService.saveLikedRedis(customerId,commentId);//这条点赞记录存入Redis
            redisTemplate.opsForHash().putIfAbsent("MAP_COMMENT_LIKED_COUNT",key,0);
            likesService.incrementLikeedCount(commentId.toString());
        }
        if(likesNum==-1){
            System.out.println("取消点赞");
            likesService.unlikeFromRedis(customerId,commentId);//这条取消点赞的记录存入Redis
            redisTemplate.opsForHash().putIfAbsent("MAP_COMMENT_LIKED_COUNT",key,0);
            likesService.decrementLikedCount(commentId.toString());
        }
//        List<Comments> commentsListFromMysql=commentsService.getCommentsByMovieIdForXQ(movieId);
//        List<Comments> commentsListFromRedis=likesService.getLikesNumFromRedisFrojsp();
//        commentsListFromMysql.addAll(commentsListFromRedis);
        //return commentsListFromMysql;

        return likesNum;
    }
    @GetMapping("likes/{id}")
    @ResponseBody
    public Likes getById(@PathVariable("id") int likesId){
        Likes likes = likesService.getById(likesId);
        return likes;
    }

//    @PostMapping("saveLikes")
//    @ResponseBody
//    public int save(Likes likes){
//        likes.setLikesCreateTime(LocalDateTime.now());
//        likes.setLikesStatus(0);
//        int rows = likesService.saveCustomer(likes);
//        return rows;
//    }
//
//    @PostMapping("updateLikes")
//    @ResponseBody
//    public int update(Likes likes){
//        int rows = likesService.saveCustomer(likes);
//        return  rows;
//    }

    @DeleteMapping("deleteLikes")
    @ResponseBody
    public int delete(@RequestParam("id") Integer likesId){
        int rows = likesService.deleteById(likesId);
        return rows;
    }
}
